package com.lightsaber.trade.as.service.planning.impl;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.dom4j.Attribute;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;

import com.lightsaber.trade.as.dao.planning.Level3PlanningDao;
import com.lightsaber.trade.as.entity.category.Category;
import com.lightsaber.trade.as.entity.planning.AdminDuty;
import com.lightsaber.trade.as.entity.planning.Level2Planning;
import com.lightsaber.trade.as.entity.planning.Level3CampusStatus;
import com.lightsaber.trade.as.entity.planning.Level3Planning;
import com.lightsaber.trade.as.entity.planning.Planning;
import com.lightsaber.trade.as.entity.planning.PlanningLevel;
import com.lightsaber.trade.as.entity.planning.PlanningStatus;
import com.lightsaber.trade.as.entity.planning.ProgrammePlanningItem;
import com.lightsaber.trade.as.entity.programme.CurriculumHour;
import com.lightsaber.trade.as.entity.programme.OfferingCampus;
import com.lightsaber.trade.as.entity.programme.Programme;
import com.lightsaber.trade.as.service.planning.AdminDutyService;
import com.lightsaber.trade.as.service.planning.Level3CampusStatusService;
import com.lightsaber.trade.as.service.planning.Level3PlanningService;
import com.lightsaber.trade.as.service.planning.PlanningService;
import com.lightsaber.trade.as.service.planning.ProgrammePlanningItemService;
import com.lightsaber.trade.as.service.programme.CurriculumHourManager;
import com.lightsaber.trade.as.service.programme.OfferingCampusManager;
import com.lightsaber.trade.as.service.programme.ProgrammeManager;
import com.lightsaber.trade.core.common.treegrid.GridDoc;
import com.lightsaber.trade.core.dao.BaseDao;
import com.lightsaber.trade.core.entity.AuditedObject;
import com.lightsaber.trade.core.entity.BaseEntity;
import com.lightsaber.trade.core.service.BaseServiceImpl;

@Service
@Component
@Transactional
public class Level3PlanningServiceImpl extends BaseServiceImpl<Level3Planning, String> implements
        Level3PlanningService {

    private static final Logger log = LoggerFactory.getLogger(Level3PlanningServiceImpl.class);

    @Autowired
    private Level3PlanningDao level3PlanningDao;
    @Autowired
    private ProgrammeManager programmeManager;
    @Autowired
    private OfferingCampusManager offeringCampusManager;
    @Autowired
    private transient ProgrammePlanningItemService programmePlanningItemService;
    @Autowired
    private transient PlanningService planningService;

    @Autowired
    private transient AdminDutyService adminDutyService;

    @Autowired
    private transient CurriculumHourManager curriculumHourManager;

    @Autowired
    private transient Level3CampusStatusService level3CampusStatusService;

    @Override
    protected BaseDao<Level3Planning, String> getDao() {
        return level3PlanningDao;
    }

    @Override
    public Level3Planning findById(final String id) {
        return level3PlanningDao.load(id);
    }

    @Override
    public void save(final Level3Planning level3Planning) {
        if (StringUtils.isBlank(level3Planning.getId())) {
            level3PlanningDao.save(level3Planning);
        } else {
            Level3Planning old = get(level3Planning.getId());
            BeanUtils.copyProperties(level3Planning, old, new String[] { BaseEntity.ID,
                    BaseEntity.VERSION, "planning", AuditedObject.CREATED_BY,
                    AuditedObject.CREATED_DATE, "resourceGroups", "dutys", "level3CampusStatus" });
            level3PlanningDao.update(old);
        }

    }

    @Override
    public Page<Level3Planning> findPage(final Level3Planning level3Planning,
            final Page<Level3Planning> page) {
        return level3PlanningDao.findPage(level3Planning, page);
    }

    @Override
    public Map<Integer, String> getAllPlanningExercise() {
        List<Integer> yearList = level3PlanningDao.getAllPlanningExercise();
        Map<Integer, String> plannerYearMap = new HashMap<Integer, String>();
        plannerYearMap.put(0, "-All-");
        for (Integer year : yearList) {
            plannerYearMap.put(year, year + "/" + (year + 1));
        }
        return plannerYearMap;
    }

    // TODO

    @Override
    public String getViewDetail(final String level3PlanningId, final String studyMode,
            final String campus, final String department, final boolean isForEndorse) {
        return null;
    }

    // One
    private int generateViewBody(final GridDoc gridDoc, final Level3Planning level3Planning,
            final String studyMode, final String campus, final String department,
            final boolean isForEndorse) {
        StringBuffer sb = new StringBuffer();

        sb.append("<Body><B>");
        int studyYear = 1;
        studyYear = generateDisciplineDetailTree(level3Planning, sb, studyMode, campus, department,
                isForEndorse);
        sb.append("</B></Body>");
        log.info("body : {}", sb.toString());
        // gridDoc.addNodeInRoot(sb.toString());
        return studyYear;
    }

    // Two
    private int generateDisciplineDetailTree(final Level3Planning level3Planning,
            final StringBuffer sb, final String studyMode, final String campus,
            final String department, final boolean isForEndorse) {

        Category category = level3Planning.getPlanning().getCategory();
        Integer exerciseYear = category.getPlanYear();
        Integer level2End = category.getLevel2End();
        Planning planning2 = planningService.findByCategoryIdAndPlanningLevel(category.getId(),
                PlanningLevel.SecondLevel);
        Level2Planning level2Planning = planning2.getLevel2Planning();

        List<String> disciplineList = programmeManager.getAllDiscipline(
                level3Planning.getPlanningExercise(), studyMode, campus, department);
        int studyYear = 1;
        for (String discipline : disciplineList) {
            sb.append("<I  Def='Tree' id='discipline_" + discipline + "' A='" + discipline + "'>");

            int year = generateDetailTree(level3Planning, sb, discipline, exerciseYear, level2End,
                    level2Planning, studyMode, campus, department, isForEndorse);
            if (year > studyYear) {
                studyYear = year;
            }

            sb.append("</I>");
        }
        return studyYear;
    }

    private int generateDetailTree(final Level3Planning level3Planning, final StringBuffer sb,
            final String discipline, final Integer exerciseYear, final Integer level3End,
            final Level2Planning level2Planning, final String studyMode, final String campus2,
            final String department, final boolean isForEndorse) {
        int studyYear = 1;
        List<String> campusList = offeringCampusManager.distinctOperatingCampus(discipline);

        for (String campus : campusList) {
            if (StringUtils.isNotBlank(campus2)) {
                if (!campus.equals(campus2)) {
                    continue;
                }
            }
            String str = "";
            if (isForEndorse) {
                str = generateLevel3CampusStatus(level3Planning, discipline, campus);
            }
            sb.append("<I  Def='Tree' id='" + level3Planning.getId() + "_" + discipline + "_"
                    + campus + "_reject" + "' A='" + campus + "' " + str + ">");

            List<String> unitList = offeringCampusManager.distinctOperatingUnit(discipline, campus);
            for (String unit : unitList) {
                if (StringUtils.isNotBlank(department)) {
                    if (!unit.equals(department)) {
                        continue;
                    }
                }
                String dutyItem = generateAdminDuty(level3Planning, discipline, campus, unit,
                        exerciseYear, level3End);

                sb.append("<I  Def='Tree' id='" + level3Planning.getId() + "_" + discipline + "_"
                        + campus + "_" + unit + "' A='" + unit + "' " + dutyItem + ">");
                List<Programme> programmeList = programmeManager.findByDisciplineAndCampusAndDep(
                        discipline, campus, unit, studyMode);
                for (Programme programme : programmeList) {
                    if (programme.getDuration() > studyYear) {
                        studyYear = programme.getDuration();
                    }
                    List<OfferingCampus> ocampusList = programme.getOfferingCampusList();
                    for (OfferingCampus oc : ocampusList) {
                        Map<String, String> map = generateYearData(level3Planning, oc,
                                programme.getDuration(), exerciseYear, level3End, level2Planning,
                                discipline, programme.getCode());
                        String stNew = map.get("New");
                        String stProg = map.get("Prog");
                        String group = map.get("group");
                        String F = "";
                        if (isForEndorse) {
                            F = " F='" + programme.getDuration() + "' ";
                        }
                        sb.append("<I  Def='Tree' id='" + level3Planning.getId() + "_"
                                + programme.getId() + "' A='" + programme.getCode() + "' B='"
                                + programme.getStudyMode() + "' C='" + programme.getStudylevel()
                                + "' D='" + programme.getTitleShort() + "' E='"
                                + programme.getMainStream() + "' " + F + group + ">");

                        String i_id = level3Planning.getId() + "_" + oc.getId();

                        sb.append("<I Def='Leaf' id='" + i_id + "_New" + "' A='New' " + stNew
                                + "/>");
                        sb.append("<I Def='Leaf' id='" + i_id + "_Prog" + "' A='Prog.' " + stProg
                                + "/>");
                        sb.append("</I>");
                    }
                    // sb.append("</I>");// programme code
                }
                sb.append("</I>");// unit
            }
            sb.append("</I>");// campus
        }
        return studyYear;
    }

    private String generateLevel3CampusStatus(final Level3Planning level3Planning,
            final String discipline, final String campus) {

        Level3CampusStatus l3cs = level3CampusStatusService.findByLevel3AndCampus(
                level3Planning.getId(), discipline, campus);

        String str = "";
        if (l3cs == null) {
            Level3CampusStatus newCS = new Level3CampusStatus();
            newCS.setDiscipline(discipline);
            newCS.setOperatingCampus(campus);
            newCS.setStatus(PlanningStatus.WaitingForEndorsement);
            newCS.setLevel3Planning(level3Planning);
            newCS.setReason("");
            level3CampusStatusService.save(newCS);
            str = " G='" + PlanningStatus.WaitingForEndorsement.getLabel()
                    + "' H='0' HCanEdit='1' KCanEdit='1' ";
        } else {
            String reason = l3cs.getReason();
            if (reason == null) {
                reason = "";
            }
            if (l3cs.getStatus().equals(PlanningStatus.Rejected)) {
                str = " G='" + l3cs.getStatus().getLabel() + "' H='1' HCanEdit='0' K='" + reason
                        + "'  KCanEdit='0' ";
            } else {
                str = " G='" + l3cs.getStatus().getLabel() + "' H='0' HCanEdit='1' K='" + reason
                        + "'  KCanEdit='1' ";
            }
        }
        return str;
    }

    private String generateAdminDuty(final Level3Planning level3Planning, final String discipline,
            final String campus, final String dep, final Integer exerciseYear,
            final Integer level3End) {

        String str = "";
        for (Integer i = exerciseYear; i < level3End + 1; i++) {
            AdminDuty duty = adminDutyService.findUniqueAdminDuty(level3Planning.getId(),
                    discipline, campus, dep, i);
            int dutyValu = 0;
            if (duty == null) {
                AdminDuty newDuty = new AdminDuty();
                newDuty.setDiscipline(discipline);
                newDuty.setOperatingCampus(campus);
                newDuty.setOperatingUnit(dep);
                newDuty.setYear(i);
                newDuty.setDuty(0);
                newDuty.setLevel3Planning(level3Planning);
                adminDutyService.save(newDuty);
            } else {
                dutyValu = duty.getDuty();
            }
            String prefix = "_" + i + "_";
            str += prefix + "duty='" + dutyValu + "'  " + prefix + "dutyCanEdit='1'  ";
        }
        return str;
    }

    // Three
    private Map<String, String> generateYearData(final Level3Planning level3Planning,
            final OfferingCampus offeringCampus, final int studyYear, final Integer exerciseYear,
            final Integer level3End, final Level2Planning level2Planning, final String discipline,
            final String code) {

        StringBuffer sbNew = new StringBuffer();
        StringBuffer sbProg = new StringBuffer();
        StringBuffer group = new StringBuffer();

        Map<String, String> map = new HashMap<String, String>();

        for (Integer year = exerciseYear; year < level3End + 1; year++) {

            AdminDuty duty = adminDutyService.findUniqueAdminDuty(level3Planning.getId(),
                    discipline, offeringCampus.getOperatingCampus(),
                    offeringCampus.getOperatingUnit(), year);

            int chHours = 0;
            for (int i = 1; i <= studyYear; i++) {
                log.info("ppi :", level2Planning.getId() + "," + offeringCampus.getId() + ","
                        + year + "," + i);
                ProgrammePlanningItem ppi = programmePlanningItemService.findUniquePpi(
                        level2Planning.getId(), code, year, i, offeringCampus.getOperatingCampus(),
                        offeringCampus.getOperatingUnit());
                if (ppi == null) {
                    continue;
                }
                String prefix = "_" + year + "_" + i;
                sbNew.append(prefix + "='" + ppi.getPlannedPlacesNew() + "' ");

                sbProg.append(prefix + "='" + ppi.getPlannedPlacesProg() + "' ");

                CurriculumHour ch = curriculumHourManager.findByProgrammeIdAndYear(offeringCampus
                        .getProgramme().getId(), i);
                int hours = 0;
                if (ch != null) {
                    hours = ch.getHours();
                    chHours += hours;
                }
                group.append(prefix
                        + "_group='&lt;input type=&#039;button&#039; class=&#039;dButton&#039; onclick=&#039;toAddGroup(event,&quot;"
                        + level3Planning.getId() + "&quot;,&quot;" + ppi.getId() + "&quot;,&quot;"
                        + duty.getId() + "&quot;," + hours + ")&#039; value=&#039;RG&#039; &gt;' ");
            }
            group.append("_" + year + "_CH='" + chHours + "' ");

            // List<Long> phHours = programmePlanningItemService
            // .sumForPlannedPlaceByLevel2IdAndCampusIdAndPlanYear(level2Planning.getId(),
            // offeringCampus.getId(), year);
            // sbNew.append("_" + year + "_CH='" + phHours.get(0) + "' ");
            // sbProg.append("_" + year + "_PH='" + phHours.get(1) + "' ");

        }
        map.put("New", sbNew.toString());
        map.put("Prog", sbProg.toString());
        map.put("group", group.toString());
        return map;
    }

    private void generateHead(final GridDoc gridDoc, final Integer planYear,
            final Integer exerciseYear, final Integer level3End, final int studyYear,
            final boolean isForEndorse) {

        StringBuffer header1 = new StringBuffer();
        StringBuffer header2 = new StringBuffer();

        int span = 5;
        String headerTemp = "";
        if (isForEndorse) {
            span = 9;
            headerTemp = " F='Dura.(Yr.)' FAlign='Center' FType='Html' "
                    + " G='Status' GAlign='Center' GType='Html' "
                    + " H='Reject' HAlign='Center' HType='Html' "
                    + " K='Reason' KAlign='Center' KType='Html' ";
        }

        header1.append("<Header Spanned='1' ASpan='" + span + "' A='Planning Exercise : "
                + planYear + "/" + (planYear + 1) + "' AType='Html' ");

        header2.append("<Header id='Header' A='Discipline/Campus/Dept./Prog. No.' AAlign='Left' AType='Html' "
                + "B='Mode' BAlign='Center' BType='Html' "
                + "C='Level' CAlign='Center' CType='Html' "
                + "D='Programme Title' DAlign='Center' DType='Html' "
                + "E='Mainstream' EAlign='Center' EType='Html' " + headerTemp);

        for (Integer i = exerciseYear; i < level3End + 1; i++) {
            String prefix = "_" + i + "_";
            String headName = i + "-" + (i + 1);

            header1.append(prefix + "dutySpan='" + (1 + studyYear * 2 + 2) + "' " + prefix
                    + "duty='" + headName + "' " + prefix + "dutyAlign='Center' " + prefix
                    + "dutyType='Html' ");
            header2.append(prefix + "duty='Admin &lt;br/&gt; duty' " + prefix
                    + "dutyAlign='Center' " + prefix + "dutyType='Html' ");
            for (int j = 1; j <= studyYear; j++) {
                String stu_prefix = prefix + j;
                header2.append(stu_prefix + "='Yr-" + j + "' " + stu_prefix + "Align='Center' "
                        + stu_prefix + "Type='Html' ");
                header2.append(stu_prefix + "_group='Yr-" + j + " (Resource Group)' " + stu_prefix
                        + "_groupAlign='Center' " + stu_prefix + "_groupType='Html' ");
            }

            header2.append(prefix + "CH" + "='(Total Curriculum Hours)' " + prefix + "CH"
                    + "Align='Center' " + prefix + "CH" + "Type='Html' ");
            header2.append(prefix + "PH" + "='(Total Planned Place)' " + prefix + "PH"
                    + "Align='Center' " + prefix + "PH" + "Type='Html' ");
        }
        header1.append("/>");
        header2.append("/>");
        log.info("header2.toString() ={}", header2.toString());
        String head = "<Head>" + header1.toString() + header2.toString() + "</Head>";
        log.info("Head ={}", head);
        // gridDoc.addNodeInRoot(head);

    }

    private void generateViewDef(final GridDoc gridDoc, final Integer level3End,
            final int studyYear, final Integer exerciseYear) {
        StringBuffer treeSb = new StringBuffer();
        StringBuffer leafSb = new StringBuffer();

        treeSb.append("<D Name='Tree' Calculated='1' ");
        leafSb.append("<D Name='Leaf' Calculated='1' ");

        for (Integer i = exerciseYear; i < level3End + 1; i++) {
            String prefix = "_" + i + "_";
            String tempName = "";
            for (int j = 1; j <= studyYear; j++) {
                treeSb.append(prefix + j + "Formula='sum()' ");
                if (j == 1) {
                    tempName = prefix + j;
                } else {
                    tempName += "+" + prefix + j;
                }
            }
            treeSb.append(prefix + "CH" + "Formula='sum()' ");
            treeSb.append(prefix + "PH" + "Formula='sum()' ");
            // leafSb.append(prefix + "CH" + "Formula='" + tempName + "' ");
            leafSb.append(prefix + "PH" + "Formula='" + tempName + "' ");
        }

        treeSb.append("/>");
        leafSb.append("/>");

        String def = "<Def>" + treeSb.toString() + leafSb.toString() + "</Def>";
        log.debug("Def ={}", def);
        // gridDoc.addNodeInRoot(def);
    }

    private void generateViewCols(final GridDoc gridDoc, final int studyYear,
            final Integer level3End, final Integer exerciseYear) {
        StringBuffer sb = new StringBuffer();
        sb.append("<Cols>");

        for (Integer i = exerciseYear; i < level3End + 1; i++) {
            String prefix = "_" + i + "_";
            sb.append("<C Name='" + prefix
                    + "duty' Width='90' Type='Float' Align='Center' CanEdit='0' />");
            for (int j = 1; j <= studyYear; j++) {
                sb.append("<C Name='" + prefix + j
                        + "' Width='90' Type='Float' Align='Center' CanEdit='0' />");
                sb.append("<C Name='" + prefix + j
                        + "_group' Width='90' Type='Html' Align='Center' CanEdit='0' />");
            }
            sb.append("<C Name='" + prefix + "CH"
                    + "' Width='90' Type='Float' Align='Center' CanEdit='0' />");
            sb.append("<C Name='" + prefix + "PH"
                    + "' Width='90' Type='Float' Align='Center' CanEdit='0' />");
        }
        sb.append("</Cols>");

        log.debug("Cols ={}", sb.toString());
        // gridDoc.addNodeInRoot(sb.toString());
    }

    private void generateViewLeftCols(final GridDoc gridDoc, final boolean isForEndorse) {
        StringBuffer sb = new StringBuffer();
        sb.append("<LeftCols>");
        sb.append("<C Name='A' Width='190' Type='Text' CanEdit='0'  AVisible='0' />");
        sb.append("<C Name='B' Width='60' Type='Text' CanEdit='0' Align='Center'  BVisible='0' />");
        sb.append("<C Name='C' Width='60' Type='Text' CanEdit='0' Align='Center'  CVisible='0' />");
        sb.append("<C Name='D' Width='60' Type='Text' CanEdit='0' Align='Center'  DVisible='0' />");
        sb.append("<C Name='E' Width='60' Type='Text' CanEdit='0' Align='Center'  EVisible='0' />");
        if (isForEndorse) {
            sb.append("<C Name='F' Width='60' Type='Text' CanEdit='0' Align='Center'  FVisible='0' />");
            sb.append("<C Name='G' Width='60' Type='Text' CanEdit='0' Align='Center'  GVisible='0' />");
            sb.append("<C Name='H' Width='60' Type='Bool' CanEdit='0' Align='Center'  HVisible='0' />");
            sb.append("<C Name='K' Width='60' Type='Lines' CanEdit='0' Align='Center'  KVisible='0' />");
        }
        sb.append("</LeftCols>");
        log.debug("LeftCols ={}", sb.toString());
        // gridDoc.addNodeInRoot(sb.toString());
    }

    @Override
    public void saveViewDetail(final String viewDetailData) {
        log.info("XML = {}", viewDetailData);
        Iterator<Element> ite;
        try {
            ite = GridDoc.parseChanges(viewDetailData);
        } catch (DocumentException e) {
            log.error(e.getMessage(), e);
            return;
        }
        while (ite.hasNext()) {
            Element I = ite.next();
            if (!StringUtils.equals("1", I.attributeValue("Changed"))) {
                continue;
            }

            String id = I.attributeValue("id");
            log.info("id: {}", id);
            if (StringUtils.isBlank(id)) {
                continue;
            }

            // discipline
            String[] ids = id.split("_");
            String leve3Id = ids[0];
            String discipline = ids[1];
            String campus = ids[2];

            Iterator<Attribute> attrs = I.attributeIterator();
            if (id.indexOf("reject") == -1) {

                String dep = ids[3];
                while (attrs.hasNext()) {
                    Attribute attr = attrs.next();

                    String cellName = attr.getName();
                    if (!cellName.startsWith("_")) {
                        continue;
                    }

                    String cellvalue = attr.getValue();
                    Integer cellvalueInt = Integer.parseInt(cellvalue);
                    String planYear = cellName.substring(1, cellName.lastIndexOf("_"));

                    log.info("cellvalue : {}", cellvalue);
                    log.info("leve3Id : {}", leve3Id);
                    log.info("discipline : {}", discipline);
                    log.info("campus : {}", campus);
                    log.info("dep : {}", dep);
                    log.info("planYear : {}", planYear);

                    AdminDuty duty = adminDutyService.findUniqueAdminDuty(leve3Id, discipline,
                            campus, dep, Integer.valueOf(planYear));
                    duty.setDuty(cellvalueInt);
                    adminDutyService.save(duty);
                }
            } else {
                while (attrs.hasNext()) {
                    Attribute attr = attrs.next();
                    String cellName = attr.getName();
                    String cellvalue = attr.getValue();
                    log.info("cellvalue : {}", cellvalue);
                    if (cellName.equals("H")) {
                        Level3CampusStatus l3cs = level3CampusStatusService.findByLevel3AndCampus(
                                leve3Id, discipline, campus);
                        l3cs.setStatus(PlanningStatus.Rejected);
                        level3CampusStatusService.save(l3cs);

                        Level3Planning level3Planning = findById(leve3Id);
                        Planning planning = level3Planning.getPlanning();
                        planning.setStatus(PlanningStatus.Rejected);
                        planningService.save(planning);

                    } else if (cellName.equals("K")) {
                        Level3CampusStatus l3cs = level3CampusStatusService.findByLevel3AndCampus(
                                leve3Id, discipline, campus);
                        l3cs.setReason(cellvalue);
                        level3CampusStatusService.save(l3cs);
                    }
                }
            }

        }
    }

    @Override
    public void confirmForDetail(final String level3PlanningId) {
        Level3Planning level3Planning = findById(level3PlanningId);
        Planning planning = level3Planning.getPlanning();
        planning.setStatus(PlanningStatus.WaitingForEndorsement);
        planningService.save(planning);

        List<Level3CampusStatus> list = level3Planning.getLevel3CampusStatus();
        for (Level3CampusStatus cs : list) {
            if (cs.getStatus().equals(PlanningStatus.Rejected)) {
                cs.setStatus(PlanningStatus.WaitingForEndorsement);
                level3CampusStatusService.save(cs);
            }
        }
    }

    @Override
    public String endorseLevel3(final String level3PlanningId) {
        Level3Planning level3Planning = findById(level3PlanningId);
        List<Level3CampusStatus> list = level3Planning.getLevel3CampusStatus();
        boolean hasReject = false;
        for (Level3CampusStatus cs : list) {
            if (cs.getStatus().equals(PlanningStatus.Rejected)) {
                hasReject = true;
                break;
            }
        }
        if (hasReject) {
            return " Some Campus Status's status is reject, so you cann't endorse the Planning! ";
        } else {
            Planning planning = level3Planning.getPlanning();
            planning.setStatus(PlanningStatus.Endorsed);
            planningService.save(planning);

            return " Endorse this Planning successfully!";
        }

    }

}
